package com.validator.response;

import lombok.Data;

import java.io.Serializable;

@Data
public class ApiResponse<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 成功标志
	 */
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	private String message = "操作成功！";

	/**
	 * 返回代码
	 */
	private Integer code = 0;
	
	/**
	 * 返回数据对象 data
	 */
	private T data;


	public ApiResponse() {
		
	}
	
	public ApiResponse error500(String message) {
		this.message = message;
		this.code = ResultCode.ERROR_500;
		this.success = false;
		return this;
	}
	
	public ApiResponse success(String message) {
		this.message = message;
		this.code = ResultCode.SUCCESS;
		this.success = true;
		return this;
	}
	
	
	public static ApiResponse<Object> ok() {
		ApiResponse<Object> r = new ApiResponse<Object>();
		r.setSuccess(true);
		r.setCode(ResultCode.SUCCESS);
		r.setMessage("success");
		return r;
	}
	
	public static ApiResponse<Object> ok(String msg) {
		ApiResponse<Object> r = new ApiResponse<Object>();
		r.setSuccess(true);
		r.setCode(ResultCode.SUCCESS);
		r.setMessage(msg);
		r.setData(msg);
		return r;
	}
	
	public static <T> ApiResponse<T> ok(T data) {
		ApiResponse<T> r = new ApiResponse<>();
		r.setSuccess(true);
		r.setCode(ResultCode.SUCCESS);
		r.setData(data);
		r.setMessage("success");
		return r;
	}


	public static ApiResponse<Object> error(ErrorEnum errorEnum) {
		return error(errorEnum.getCode(), errorEnum.getMessage());
	}

	public static ApiResponse<Object> error(String msg) {
		return error(ResultCode.ERROR_500, msg);
	}

	public static ApiResponse<Object> error503(String msg) {
		return error(ResultCode.ERROR_503, msg);
	}
	
	public static ApiResponse<Object> error(int code, String msg) {
		ApiResponse<Object> r = new ApiResponse<Object>();
		r.setCode(code);
		r.setMessage(msg);
		r.setSuccess(false);
		return r;
	}

	public static ApiResponse<Object> error(String data, String msg) {
		ApiResponse<Object> r = new ApiResponse<Object>();
		r.setData(data);
		r.setMessage(msg);
		r.setSuccess(false);
		return r;
	}

	public static ApiResponse<Object> ok(String data, String msg) {
		ApiResponse<Object> r = new ApiResponse<Object>();
		r.setData(data);
		r.setMessage(msg);
		r.setSuccess(true);
		return r;
	}

}